package com.ruoyi.mes.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.PlnMaterialPlan;
import com.ruoyi.mes.domain.PlnReleasedPlan;
import com.ruoyi.mes.domain.SleOrderPro;
import com.ruoyi.mes.mapper.PlnMaterialPlanMapper;
import com.ruoyi.mes.mapper.PlnReleasedPlanMapper;
import com.ruoyi.mes.mapper.SleOrderProMapper;
import com.ruoyi.mes.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.mapper.PlnMasterPlanMapper;
import com.ruoyi.mes.domain.PlnMasterPlan;
import com.ruoyi.mes.service.IPlnMasterPlanService;

/**
 * 生产计划Service业务层处理
 * 
 * @author CFL
 * @date 2024-04-28
 */
@Service
public class PlnMasterPlanServiceImpl implements IPlnMasterPlanService
{
    @Autowired
    private PlnMasterPlanMapper plnMasterPlanMapper;

    @Autowired
    private PlnReleasedPlanMapper plnReleasedPlanMapper;

    @Autowired
    private PlnMaterialPlanMapper plnMaterialPlanMapper;

    @Autowired
    private SleOrderProMapper sleOrderProMapper;
    /**
     * 查询生产计划
     * 
     * @param orderId 销售订单主键
     * @return 生产计划
     */
    @Override
    public List<PlnMasterPlan> selectPlnMasterPlanById(Long orderId)
    {
        return plnMasterPlanMapper.selectPlnMasterPlanById(orderId);
    }

    /**
     * 查询生产计划列表
     * 
     * @param plnMasterPlan 生产计划
     * @return 生产计划
     */
    @Override
    public List<PlnMasterPlan> selectPlnMasterPlanList(PlnMasterPlan plnMasterPlan)
    {
        return plnMasterPlanMapper.selectPlnMasterPlanList(plnMasterPlan);
    }

    /**
     * 查询下达计划列表
     *
     * @param orderId 生产计划
     * @return 生产计划集合
     */
    public List<PlnReleasedPlan> selectPlnReleasedPlanListByOrderId(Long orderId)
    {

        return plnReleasedPlanMapper.selectPlnReleasedPlanListByOrderId(orderId);
    }

    /**
     * 新增生产计划
     * 
     * @param plnMasterPlan 生产计划
     * @return 结果
     */
    @Override
    public int insertPlnMasterPlan(PlnMasterPlan plnMasterPlan)
    {
        return plnMasterPlanMapper.insertPlnMasterPlan(plnMasterPlan);
    }

    /**
     * 修改生产计划
     *
     * @param plnMasterPlan 生产计划
     * @return 结果
     */
//    @Override
//    public int updatePlnMasterPlan(PlnMasterPlan plnMasterPlan)
//    {
//        return plnMasterPlanMapper.updatePlnMasterPlan(plnMasterPlan);
//    }
    /**
     * 修改生产计划
     *
     * @param plnMasterPlan 生产计划
     * @return 结果
     */
    @Override
    public int updatePlnMasterPlan(List<PlnMasterPlan> plnMasterPlan)
    {
        return plnMasterPlanMapper.updatePlnMasterPlan(plnMasterPlan);
    }

    /**
     * 新增下达计划
     *
     * @param plnMasterPlan 生产计划
     * @return 结果
     */
    @Override
    public AjaxResult addPlnMasterPlan(PlnMasterPlan plnMasterPlan)
    {
        BaseController baseController = new BaseController();
        String username = baseController.getUsername();

            PlnReleasedPlan plnReleasedPlan = new PlnReleasedPlan();
//            //很经典的先删后增
////            sleOrderProMapper.subReleasedNum(plnMasterPlan2);
//            plnReleasedPlanMapper.deletePlnReleasedPlanById(plnMasterPlan.getId());
            //插入下达计划表
            plnReleasedPlan.setOrderId(plnMasterPlan.getOrderId());
            plnReleasedPlan.setProId(plnMasterPlan.getProId());
            plnReleasedPlan.setNum(plnMasterPlan.getNum());
            plnReleasedPlan.setReleasedDate(DateUtils.getNowDate());
            sleOrderProMapper.addReleasedNum(plnMasterPlan);
            plnReleasedPlan.setEndDate(plnMasterPlan.getPlanEndDate());
            plnReleasedPlan.setStartDate(plnMasterPlan.getPlanStartDate());
            plnReleasedPlan.setProId(plnMasterPlan.getProId());
            plnReleasedPlan.setState(plnMasterPlan.getState());
            plnReleasedPlanMapper.insertPlnReleasedPlan(plnReleasedPlan);

        return AjaxResult.success("新增成功");
    }

    /**
     * 撤回下达计划
     *
     * @param id 下达计划id
     * @return 结果
     */
    public AjaxResult subPlnMasterPlan(Long id)
    {
        PlnReleasedPlan plnReleasedPlan = new PlnReleasedPlan();
        plnReleasedPlanMapper.updatePlnReleasedPlanState(id);
        plnMaterialPlanMapper.deletePlnMaterialPlanByReleasedId(id);
        return AjaxResult.success("修改成功");
    }

//    /**
//     * 新增下达计划
//     *
//     * @param plnMasterPlan 生产计划
//     * @return 结果
//     */
//    public AjaxResult addPlnMasterPlan(PlnMasterPlan plnMasterPlan)
//    {
//        BaseController baseController = new BaseController();
//        String username = baseController.getUsername();
//
//        plnReleasedPlanMapper.
//        return AjaxResult.success("新增成功");
//    }
    /**
     * 批量删除生产计划
     * 
     * @param ids 需要删除的生产计划主键
     * @return 结果
     */
    @Override
    public int deletePlnMasterPlanByIds(Long[] ids)
    {
        return plnMasterPlanMapper.deletePlnMasterPlanByIds(ids);
    }

    /**
     * 删除生产计划信息
     * 
     * @param id 生产计划主键
     * @return 结果
     */
    @Override
    public int deletePlnMasterPlanById(Long id)
    {
        return plnMasterPlanMapper.deletePlnMasterPlanById(id);
    }
}
